<script setup lang="ts">
import {NOTICE_STATUS, NOTICE_TYPE} from "~/const/Maps";
const route=useRoute();
definePageMeta({
  layout:'admin',
  title:'公告列表'
});
const tags=useTagsStore()
//@ts-ignore
const title=ref(NOTICE_TYPE[route.query.type??10] as string)

const form=reactive({
  limit:20,
  page:1,
  type:route.query.type??10,
  is_up:3,
});

const {data:list,refresh}=await useServerGet('/backend/notice/getList',form)
const tabs=useTabsStore();

const router=useRouter();
const goDetail=(id:any)=>{
  router.push({path:'/backend/notice/publish',query:{id,type:form.type}})
}
const deleteNotice= (id:number)=>{
  ElMessageBox.confirm('确定删除吗?','提示',{
    confirmButtonText:'确定',
    cancelButtonText:'取消',
    type:'warning'
  }).then( async ()=>{
    const {data,code}=await usePost('/backend/notice/delete',{id})
    if(code===200){
      ElMessage.success('删除成功')
      refresh?.()
    }
  })

}
const cancelWatch=watch(()=>route.query.type,()=>{
  //@ts-ignore
    title.value=NOTICE_TYPE[route.query.type] as string
    form.type=route.query.type as string
    tags.setTitle(route.path,`${title.value}列表`)
  refresh?.()
})
onUnmounted((()=>{
  cancelWatch()
}))
</script>
<template>
<div>
  <el-button @click="goDetail('')" type="primary" class="mb-4">创建{{title}}</el-button>
  <el-card class="mt-4">
    <el-form :model="form" label-width="0" inline class="my" >
      <el-form-item >
        <el-select v-model="form.type" class="w-[150px!important]">
          <el-option :label="label" :value="value" v-for="(label,value) in NOTICE_TYPE"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item >
        <el-select v-model="form.is_up" class="w-[150px!important]">
          <el-option :label="label" :value="value*1" v-for="(label,value) in NOTICE_STATUS"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" @click="refresh">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list.data.list" style="width: 100%" :header-cell-style="{'text-align':'center'}" :cell-style="{'text-align':'center'}">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column label="内容">
        <template #default="{row}">
          <div class="flex">
            <div class="line-clamp-2">{{row.synopsis}}</div>
            <NuxtLink :to="`/noticeDetail/${row.id}.html`" target="_blank" class="line-clamp-2">点击查看</NuxtLink>
          </div>

        </template>
      </el-table-column>
      <el-table-column prop="type" label="类型">
        <template #default="{row}">
          <span>{{NOTICE_TYPE[row.type]}}</span>
        </template>
      </el-table-column>
      <el-table-column label="封面图">
        <template #default="{row}">
          <my-image :src="row.img" :list="[row.img]" style="width:100px;height:100px"></my-image>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="是否上线">
        <template #default="{row}">
          <el-tag  :type="row.status==1?'success':''">{{ NOTICE_STATUS[row.status] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="updated_at" label="更新时间" ></el-table-column>
      <el-table-column prop="created_at" label="创建时间" ></el-table-column>
      <el-table-column label="操作">
        <template #default="{row}">
          <el-button type="text" size="mini" @click="goDetail(row.id)">编辑</el-button>
          <el-button type="text" size="mini" @click="deleteNotice(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <MyPage v-model:page="form.page" v-model:perpage="form.limit" v-model:total="list.data.total" @change="refresh"></MyPage>
  </el-card>
</div>
</template>

<style scoped lang="scss">

</style>